import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wu_you/data/repository/trad_repository.dart';
import 'package:wu_you/l10n/messages.dart';
import 'package:wu_you/model/user/trad_model.dart';
import 'package:wu_you/page/trad/page_trad_payment.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/state/bloc/http_bloc.dart';
import 'package:wu_you/state/bloc/trad_bloc.dart';
import 'package:wu_you/utils/navigator_util.dart';
import 'package:wu_you/utils/utils.dart';
import 'package:wu_you/widgets/http_list_widget.dart';
import 'package:wu_you/utils/extension_utils.dart';

///收款方式
class PaymentMethedPage extends StatefulWidget{
  @override
  PaymentMethedPageState createState() => PaymentMethedPageState();
}
class PaymentMethedPageState extends State<PaymentMethedPage>{
  var message;

  @override
  void initState() {
    super.initState();
  }
  BuildContext _ctx;
  @override
  Widget build(BuildContext context) {
    message = AppLocalizations.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("${message['text27']}"),
      ),
      backgroundColor: Colors.white,
      body: HttpListWidget<PayMethodStatus, PaymentAllBloc>(
        creator: (context) => PaymentAllBloc(),
        onObtainData: (context, page) {
          _ctx = context;
          BlocProvider.of<PaymentAllBloc>(context).add(PageSizeHttpEvent(1,100));
        },
        successBuilder: (context, data) {
          return ListView.builder(
            itemBuilder: (context,index){
              PayMethodStatus item = data[index];
              return Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  SizedBox(height: 3,),
                  ListTile(
                    leading: _leadIcon(item),
                    title: _titleText(item),
                    trailing: _trailingText(item),
                  ),
                  SizedBox(height: 3,),
                  Divider(color: HexColor("#eeeeee"),)
                ],
              ).addGesture(() {
                if(item.payment == 1){
                  NavigatorUtil.navigateToPage(context, BankPaymentView(index: 1,));
                }else if(item.payment == 2){
                  NavigatorUtil.navigateToPage(context, ZfbPaymentView(index: 2));
                }else if(item.payment == 3){
                  NavigatorUtil.navigateToPage(context, WxPaymentView(index: 3,));
                }else if(item.payment == 4){
                  NavigatorUtil.navigateToPage(context, UsdtPaymentView(index: 4));
                }else if(item.payment == 5){
                  NavigatorUtil.navigateToPage(context, TngPaymentView(index: 5));
                }
              });
            },
            itemCount: data.length,
          );
        }
      )
    );
  }

  Widget _leadIcon(PayMethodStatus item){
    Widget child;
    if(item.payment == 1){
      child = Image.asset(Utils.getImgPath('payment_one'),width: 32,height: 32,);
    }else if(item.payment == 2){
      child = Image.asset(Utils.getImgPath('payment_two'),width: 32,height: 32,);
    }else if(item.payment == 3){
      child = Image.asset(Utils.getImgPath('payment_three'),width: 32,height: 32,);
    }else if(item.payment == 4){
      child = Image.asset(Utils.getImgPath('payment_four'),width: 32,height: 32,);
    }else if(item.payment == 5){
      child = Image.asset(Utils.getImgPath('payment_five'),width: 32,height: 32,);
    }
    return child;
  }

  Widget _titleText(PayMethodStatus item){
    Widget child;
    if(item.payment == 1){
      child = Text("${message['text22']}",style: TextStyle(fontSize: 16,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }else if(item.payment == 2){
      child = Text("${message['text23']}",style: TextStyle(fontSize: 16,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }else if(item.payment == 3){
      child = Text("${message['text24']}",style: TextStyle(fontSize: 16,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }else if(item.payment == 4){
      child = Text("${message['text26']}",style: TextStyle(fontSize: 16,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }else if(item.payment == 5){
      child = Text("${message['text30']}",style: TextStyle(fontSize: 16,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }
    return child;
  }

  Widget _trailingText(PayMethodStatus item){
    String text = '';
    Color color;
    if(item.status == "0"){
      text = "${message['text35']}";
      color = AppColors.primaryColor;
    }else if(item.status == "1"){
      text = "${message['text33']}";
      color = HexColor("#FEED35");
    }else if(item.status == "2"){
      text = "${message['text34']}";
      color = Colors.green;
    }else if(item.status == "3"){
      text = "${message['text33']}";
      color = HexColor("#FEED35");
    }else if(item.status == null){
      text = '${message['text32']}';
      color = HexColor("#999999");
    }
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 4,vertical: 2),
      child: Text(text,style: TextStyle(fontSize: 12,color: color),),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(
          color: color,
          width: 1,
        ),
      ),
    );
  }
}